package com.twu.biblioteca;

import java.util.*;


public class BibliotecaApp {


    public static void main(String[] args) {
        String userName = null;
        String password = null;

        System.out.println("Welcome to Biblioteca");
        System.out.println();

        mainMenu();


        System.out.println(new MenuItem("5 - To exit the program").retrieveNameOfMenuItem());

        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter your preferred option: ");

        try {



        while (true && !userInput.equals(4)) {
            int positionOfMenuItem = userInput.nextInt();
            BookBank bookInStore = new BookBank();
            if (positionOfMenuItem == 1)
                System.out.println(String.valueOf(bookInStore.storeForAvailableBooks()));

            else if (positionOfMenuItem == 2) {
                movieList();

            } else if (positionOfMenuItem == 3) {
                System.out.println("Please talk to the Librarian. Thank you");

            } else if (positionOfMenuItem == 4) {
                System.out.println("Enter your username: ");
                userInput.nextLine();
                userName = userInput.nextLine();
                System.out.println("Enter your password: ");
                password = userInput.nextLine();

                if (Login.loginUser(userName, password) == true) {
                    System.out.println("you have logged in successfully");
                    operationsMenu();
                } else {
                    System.out.println("May be you are not registered.");
                }
            } else if (positionOfMenuItem == 5) {
                break;

            } else if (positionOfMenuItem == 6) {
                reserveBookInLibrary(userInput, bookInStore);

            } else if (positionOfMenuItem == 7) {
                System.out.println(Login.getMembershipDetailsOnLogin(userName, password));

            } else {
                System.out.println("Select a valid option!!");
            }
        }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    private static void reserveBookInLibrary(Scanner userInput, BookBank bookInStore) {
        System.out.println("Enter the name of the book you want to reserve.");
        userInput.nextLine();
        String nameOfBookFromUser = userInput.nextLine();
        BookBank bookBank = new BookBank();
        existenceOfBook(bookInStore, nameOfBookFromUser, bookBank);
    }

    private static void operationsMenu() {
        System.out.println("Movie Library Menu");
        System.out.println(new MenuItem("6 - Reserve a book").retrieveNameOfMenuItem());
        System.out.println(new MenuItem("7 - View Membership details").retrieveNameOfMenuItem());
    }

    private static void mainMenu() {
        System.out.println("Below is our Menu:");
        System.out.println(new MenuItem("1 - Books Available").retrieveNameOfMenuItem());
        System.out.println(new MenuItem("2 - Movies").retrieveNameOfMenuItem());
        System.out.println(new MenuItem("3 - Membership details").retrieveNameOfMenuItem());
        System.out.println(new MenuItem("4 - Login").retrieveNameOfMenuItem());
        System.out.println("");
    }

    private static void movieList() {
        ArrayList<Movie> movies = displayMovies();
        System.out.println(String.format("%-25s %-2s %-25s %-25s", "Movie", "Year", "Director", "Rating"));
        for (Movie movy : movies) {

            System.out.println(movy.movieInformation());
        }
    }

    private static ArrayList<Movie> displayMovies() {
        return new MovieStore().availableMoviesInStore();
    }

    private static void existenceOfBook(BookBank bookInStore, String nameOfBookFromUser, BookBank bookBank) {
        if (bookBank.checkIfBookExistsInLibrary(new Book(nameOfBookFromUser), bookInStore.storeForAvailableBooks()) == true) {
            reserveBook(nameOfBookFromUser);
        } else {
            System.out.println("Sorry we don't have that book yet.");
        }
    }

    private static void reserveBook(String nameOfBookFromUser) {
        Book bookEntered = new Book(nameOfBookFromUser);
        if (bookEntered.checkAvailabilityOfBook() == true) {
            System.out.println("Sorry, this book is reserved");
        } else {
            System.out.print("Thank You! Enjoy the book.");
        }
    }

}
